package com.hifar.test.api.model;

import com.hifar.test.api.constant.EmployeeValidationErrorCode;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工数据验证结果
 * 包含详细的验证错误信息
 */
public class EmployeeValidationResult {
    
    private boolean valid;
    private List<EmployeeValidationErrorCode.EmployeeValidationErrorResponse> errors;
    private int totalCount;
    private int validCount;
    private int invalidCount;
    
    public EmployeeValidationResult() {
        this.valid = true;
        this.errors = new ArrayList<>();
        this.totalCount = 0;
        this.validCount = 0;
        this.invalidCount = 0;
    }
    
    public EmployeeValidationResult(boolean valid) {
        this();
        this.valid = valid;
    }
    
    /**
     * 添加验证错误
     */
    public void addError(EmployeeValidationErrorCode errorCode) {
        this.valid = false;
        this.errors.add(errorCode.createErrorResponse());
        this.invalidCount++;
    }
    
    /**
     * 添加验证错误（带字段信息）
     */
    public void addError(EmployeeValidationErrorCode errorCode, String fieldName, Object fieldValue) {
        this.valid = false;
        this.errors.add(new EmployeeValidationErrorCode.EmployeeValidationErrorResponse(errorCode, fieldName, fieldValue));
        this.invalidCount++;
    }
    
    /**
     * 添加验证错误（带索引信息）
     */
    public void addError(EmployeeValidationErrorCode errorCode, int index, String fieldName, Object fieldValue) {
        this.valid = false;
        EmployeeValidationErrorCode.EmployeeValidationErrorResponse error = 
            new EmployeeValidationErrorCode.EmployeeValidationErrorResponse(errorCode, fieldName, fieldValue);
        error.setFieldName("data[" + index + "]." + fieldName);
        this.errors.add(error);
        this.invalidCount++;
    }
    
    /**
     * 标记验证成功
     */
    public void markValid() {
        this.validCount++;
    }
    
    /**
     * 设置总数
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    
    /**
     * 检查是否有错误
     */
    public boolean hasErrors() {
        return !errors.isEmpty();
    }
    
    /**
     * 获取错误数量
     */
    public int getErrorCount() {
        return errors.size();
    }
    
    /**
     * 获取验证成功率
     */
    public double getSuccessRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) validCount / totalCount * 100;
    }
    
    // Getters and Setters
    public boolean isValid() {
        return valid;
    }
    
    public void setValid(boolean valid) {
        this.valid = valid;
    }
    
    public List<EmployeeValidationErrorCode.EmployeeValidationErrorResponse> getErrors() {
        return errors;
    }
    
    public void setErrors(List<EmployeeValidationErrorCode.EmployeeValidationErrorResponse> errors) {
        this.errors = errors;
    }
    
    public int getTotalCount() {
        return totalCount;
    }
    
    public int getValidCount() {
        return validCount;
    }
    
    public void setValidCount(int validCount) {
        this.validCount = validCount;
    }
    
    public int getInvalidCount() {
        return invalidCount;
    }
    
    public void setInvalidCount(int invalidCount) {
        this.invalidCount = invalidCount;
    }
    
    @Override
    public String toString() {
        return "EmployeeValidationResult{" +
                "valid=" + valid +
                ", errors=" + errors.size() +
                ", totalCount=" + totalCount +
                ", validCount=" + validCount +
                ", invalidCount=" + invalidCount +
                ", successRate=" + String.format("%.2f", getSuccessRate()) + "%" +
                '}';
    }
}
